package com.tdkj.service.risevalue.impl;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Table;

import org.springframework.stereotype.Component;

import com.tdkj.dao.risevalue.IBeanAdvertAuctionDao;
import com.tdkj.model.risevalue.BeanAdvertAuctionBidInfo;
import com.tdkj.model.risevalue.BeanAdvertAuctionInfo;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.BaseService;
import com.tdkj.service.ServiceException;
import com.tdkj.service.financial.IBeanFimamcialAmountChangeRecordService;
import com.tdkj.service.risevalue.IBeanAdvertAuctionService;
            
@Component
public class BeanAdvertAuctionServiceImpl extends BaseService implements IBeanAdvertAuctionService{
	
	private IBeanFimamcialAmountChangeRecordService fimamcialAmountChangeRecordService;
	
	@Resource
	public void setFimamcialAmountChangeRecordService(
			IBeanFimamcialAmountChangeRecordService fimamcialAmountChangeRecordService) {
		this.fimamcialAmountChangeRecordService = fimamcialAmountChangeRecordService;
	}

	private IBeanAdvertAuctionDao auctionDao;

	@Resource
	public void setAuctionDao(IBeanAdvertAuctionDao auctionDao) {
		this.auctionDao = auctionDao;
	}

	@Override
	public void createBid(Long userId, Long auctionId, Integer price) throws ServiceException {
		BeanUser user = this.getBaseDao().findById(userId, BeanUser.class);
		BeanAdvertAuctionInfo auction = this.getBaseDao().findById(auctionId, BeanAdvertAuctionInfo.class);
		
		String hql = "from " + BeanAdvertAuctionBidInfo.class.getName() + " as bean where bean.auctioninfo.id='"+auctionId+"' and bean.user.id='"+userId+"' order by bean.auctiontime";
		List<BeanAdvertAuctionBidInfo> bidInfos = this.getBaseDao().getSession().createQuery(hql).list();
		
		BeanAdvertAuctionBidInfo lastBidInfo = null;
		if(bidInfos!=null && bidInfos.size()>0){
			lastBidInfo = bidInfos.get(0);
		}
		
		if(lastBidInfo != null){
			fimamcialAmountChangeRecordService.createIn(userId, lastBidInfo.getPrice(), auction.getCurrency(), lastBidInfo.getId(), BeanAdvertAuctionBidInfo.class, "返还冻结资金："+lastBidInfo.getPrice()+auction.getCurrency());
		}
		
		lastBidInfo = new BeanAdvertAuctionBidInfo();
		lastBidInfo.setAuctioninfo(auction);
		lastBidInfo.setPrice(price);
		lastBidInfo.setUser(user);
		lastBidInfo.setAuctiontime(new Timestamp(System.currentTimeMillis()));
		lastBidInfo.setStatus("未成功");
		
		this.getBaseDao().save(lastBidInfo);
		
		fimamcialAmountChangeRecordService.createOut(userId, lastBidInfo.getPrice(), auction.getCurrency(), lastBidInfo.getId(), BeanAdvertAuctionBidInfo.class, "扣除冻结资金："+lastBidInfo.getPrice()+auction.getCurrency());
	}

	@Override
	public void endAuction(Long aid) throws ServiceException {
		BeanAdvertAuctionInfo a=this.findById(aid, BeanAdvertAuctionInfo.class);
		a.setStatus("竞价结束");
		String sql = " select bidInfo.userid as user_id, max(bidInfo.price) as last_price from " + BeanAdvertAuctionBidInfo.class.getAnnotation(Table.class).name() +" as bidInfo where bidInfo.auctionInfoid='"+aid+"' group by bidInfo.userId";
		List<Object[]> bidInfos = this.getBaseDao().getSession().createSQLQuery(sql).list();
		for(Object[] bidInfo : bidInfos){
			Long userId = ((BigInteger)bidInfo[0]).longValue();
			Integer lastPrice = ((BigInteger)bidInfo[0]).intValue();
			
			fimamcialAmountChangeRecordService.createIn(userId, lastPrice, a.getCurrency(), a.getId(), BeanAdvertAuctionInfo.class, "竞价结束");
		}
		
		String hql = " from " + BeanAdvertAuctionBidInfo.class.getName() +" as bidInfo where bidInfo.auctioninfo.id='"+aid+"' order by bidInfo.price desc,bidInfo.auctiontime asc";
		BeanAdvertAuctionBidInfo resultBidInfo = (BeanAdvertAuctionBidInfo)this.getBaseDao().getSession().createQuery(hql).setMaxResults(1).uniqueResult();
		
		if(resultBidInfo == null){
			throw new ServiceException("本广告位没有 人参与竞价");
		}
		a.setUser(resultBidInfo.getUser());
		fimamcialAmountChangeRecordService.createOut(resultBidInfo.getUser().getId(), resultBidInfo.getPrice(), a.getCurrency(), a.getId(), BeanAdvertAuctionInfo.class, "竞价成功");
	}

	@Override
	public void endWorking(Long aid) {
		// TODO Auto-generated method stub
		
	}

	
}
